package yamen.bdwm.board;

import yamen.bdwm.BoardFragment;
import yamen.bdwm.MainActivity;
import yamen.bdwm.R;
import yamen.bdwm.compatible.ActionBarActivity;
import yamen.bdwm.data.WmMyData;
import yamen.bdwm.data.interfaces.onListRefreshedListener;
import yamen.bdwm.datastruct.WmBoardItem;
import yamen.bdwm.datastruct.WmDataAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FavouriteListFragment extends ListFragment implements
		onListRefreshedListener {

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.list, container, false);
		WmMyData.getinstance().favlist.CreateAdapter(0, R.layout.boarditem);
		final WmDataAdapter adapter = WmMyData.getinstance().favlist
				.getAdapter(0, this.getActivity());

		((ListView) v.findViewById(R.id.listView1)).setAdapter(adapter);

		((ListView) v.findViewById(R.id.listView1))
				.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Fragment f = ((MainActivity) FavouriteListFragment.this
								.getActivity()).getCurrentFragment();
						if (f instanceof BoardFragment) {

							((BoardFragment) f)
									.showThreads((WmBoardItem) adapter
											.getItem(position));
						}
					}

				});
		if (adapter.getCount() == 0) {
			if(WmMyData.getinstance().parser.is_login())
			{
				//((ActionBarActivity) this.getActivity()).SetRefreshingDialog(true);
				WmMyData.getinstance().favlist.getRefreshed(this);
			}
		}
		return v;

	}

	public void onRefreshed(int numberOfRefreshed) {
		((ActionBarActivity) this.getActivity()).SetRefreshingDialog(false);
		if (getActivity() instanceof ActionBarActivity) {
			((ActionBarActivity) getActivity()).getActionBarHelper()
					.setRefreshActionItemState(false);
		}
	}

	public void onError(int i) {
		if (this.getActivity() != null) {
			((ActionBarActivity) this.getActivity()).SetRefreshingDialog(false);
			if (getActivity() instanceof ActionBarActivity) {
				((ActionBarActivity) getActivity()).getActionBarHelper()
						.setRefreshActionItemState(false);
			}
		}

	}

}
